package com.gict.antifraudback.utils;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateUtil {

    /**
     * 日期转字符串
     *
     * @param date
     * @return
     */
    public static String getString(Date date) {
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return ft.format(date);
    }

    /**
     * 日期转字符串
     *
     * @param date
     * @return
     */
    public static String getStringTwo(Date date) {
        SimpleDateFormat ft = new SimpleDateFormat("yyyyMMddHHmmss");
        return ft.format(date);
    }

    /**
     * 日期转字符串
     *
     * @param date
     * @return
     */
    public static String getStringTime(Date date) {
        SimpleDateFormat ft = new SimpleDateFormat("HH:mm:ss");
        return ft.format(date);
    }

    /**
     * 日期转字符串
     *
     * @param date
     * @return
     */
    public static String getStringDate(Date date) {
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        return ft.format(date);
    }

    /**
     * 字符串转时间对象
     *
     * @param str
     * @return
     */
    public static Date getDate(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date;
    }


    /**
     * 字符串转时间对象
     *
     * @param str
     * @return
     */
    public static Date getDate(String str, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 返还一个日历类的对象
     *
     * @param date
     * @return
     */
    public static Calendar getCalendar(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c;
    }

    /**
     * 计算两个时间之间
     * @param endDate
     * @param nowDate
     * @return
     */
    public static String getDatePoor(Date nowDate,Date endDate) {

        long nd = 1000 * 24 * 60 * 60;//每天毫秒数

        long nh = 1000 * 60 * 60;//每小时毫秒数

        long nm = 1000 * 60;//每分钟毫秒数

        long sm = 1000;   //每秒钟毫秒数

        long diff = endDate.getTime() - nowDate.getTime(); // 获得两个时间的毫秒时间差异

        long day = diff / nd;   // 计算差多少天

        long hour = diff % nd / nh; // 计算差多少小时

        long min = diff % nd % nh / nm; // 计算差多少分钟

        long second = diff % nd % nh % nm / sm;   //计算差多少秒

        StringBuilder builder = new StringBuilder();

        String dayString = new String(day+"天");
        String hourString  = new String(hour+"小时");
        String minString = new String(min+"分钟");
        String secondString = new String(second+"秒");

        if(day!=0){
            builder.append(dayString);
        }

        if(hour!=0){
            builder.append(hourString);
        }
        if(min!=0){
            builder.append(minString);
        }
        if(second!=0){
            builder.append(secondString);
        }
        return builder.toString();
    }

    public static void main(String[] args) {
        Date date  = DateUtil.getDate("2020-06-01 9:25:41");
        Date date1 = DateUtil.getDate("2020-06-01 9:26:33");
        System.out.println(DateUtil.getDatePoor(date,date1));
    }

}
